// 创建用户相关的小仓库
import { reqAddUser, reqLogin, reqUserAll, reqUserById, reqUserDelete, reqUserList, reqUserUpdate } from "@/api/user";
import type { loginFormData, loginResponseData, userAllResponesData, userInfoData, userListFormData, userListResponesData } from "@/api/user/type";
import { defineStore } from "pinia";
import type { UserState } from "./types/types";
let useUserStore = defineStore("User",{
    state:():UserState=>{
        return{
    token: localStorage.getItem('b0445-vue3-token') || '',
    user: JSON.parse(localStorage.getItem('B0445-STU-USER') || '{}'),
    users: []
}
    },
    actions:{
        // 登录
        async userLogin(data:loginFormData){
            // 调用api里面的真正发送请求的方法，实现登录逻辑
            let result:loginResponseData = await reqLogin(data)
            if(result.status == 0){
                this.token = result.data?.token as string
                // 进行本地持久化存储
                localStorage.setItem('b0445-vue3-token',this.token)
                // @ts-ignore
                this.user = result.data?.user as userInfoData
                localStorage.setItem("B0445-STU-USER",JSON.stringify( this.user))
                return 'ok'
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        removeUser(){
            this.user = null;
            this.token = "";
            localStorage.removeItem('b0445-vue3-token')
            localStorage.removeItem("B0445-STU-USER")
        },
        async getUserAll(){
             let result:userAllResponesData = await reqUserAll()
            if(result.status == 0){
               this.users =(result.data as userInfoData[])
                return 'ok'
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async getUserList(data:userListFormData){
             let result:userListResponesData = await reqUserList(data)
            if(result.status == 0){
               this.users = (result.data?.data as userInfoData[])
               return result.data
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        // 添加角色
        async addUser(data: userInfoData) {


            let result: loginResponseData = await reqAddUser(data);
            if (result.status == 0) {
                this.getUserAll()
                return "ok"
            } else { 
                return Promise.reject(new Error(result.msg))
            }
        },
        // 根据id查询
        async getUserById(_id:string){
             let result: loginResponseData = await reqUserById(_id);
            if (result.status == 0) {
                return result.data
            } else {
           
                
                return Promise.reject(new Error(result.msg))
            }
        },
        // 修改用户
        async updateUser(data: userInfoData) {


            let result: loginResponseData = await reqUserUpdate(data);
            if (result.status == 0) {
                this.getUserAll()
                return "ok"
            } else { 
                return Promise.reject(new Error(result.msg))
            }
        },
        // 删除用户
        async deleteUser(userId:string) {
            let result: loginResponseData = await reqUserDelete(userId);
            if (result.status == 0) {
                this.getUserAll()
                return "ok"
            } else { 
                return Promise.reject(new Error(result.msg))
            }
        },
    }
})
export default useUserStore